package com.woyou.ui.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.citaq.ideliver.R;
import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.OnPreRefreshingAnimListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.woyou.bean.Comments;
import com.woyou.bean.CommentsCon;
import com.woyou.bean.Result;
import com.woyou.bean.ShopItem;
import com.woyou.bean.rpc.CommentsReq;
import com.woyou.model.ShopsModel_;
import com.woyou.service.NetWorkCenter;
import com.woyou.service.ThreadPoolManager_;
import com.woyou.ui.adapter.ShopCommentAdapter;
import com.woyou.ui.component.ErrorHintView;
import com.woyou.ui.component.ErrorHintView.OperateListener;
import com.woyou.ui.view.SuperLayout;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import retrofit.RetrofitError;

/**
 * 店铺评论
 * 
 * @author shenzhou 2014年12月25日
 */
public class ShopInfoCommentLayout extends SuperLayout implements OnClickListener{
	
	public static final String TAG = "ShopInfoCommentLayout";
	
	/** 下一页的页码，默认为第一页 **/
	private int allNext = 1;
	private int goodNext = 1;
	private int middleNext = 1;
	private int badNext = 1;
	
	private TextView all, good, middle, bad;
	private PullToRefreshListView allList, goodList, middleList, badList;
	private ShopItem item;
	private ErrorHintView mHeadErrorHintView;
	private ErrorHintView mMiddleErrorHintView;
	private ErrorHintView mBadErrorHintView;
	private int position = 0; //默认是全部评论
	
	private ShopCommentAdapter allAdapter;
	private List<CommentsCon> allData = new ArrayList<CommentsCon>(); // all listview数据源
	private ShopCommentAdapter goodAdapter;
	private List<CommentsCon> goodData = new ArrayList<CommentsCon>(); // good listview数据源
	private ShopCommentAdapter middleAdapter;
	private List<CommentsCon> middleData = new ArrayList<CommentsCon>(); // middle listview数据源
	private ShopCommentAdapter badAdapter;
	private List<CommentsCon> badData = new ArrayList<CommentsCon>(); // bad listview数据源
	
	/** 显示评论列表 **/
	private int VIEW_ALL    =1;
	private int VIEW_GOOD   =2;
	private int VIEW_MIDDLE =3;
	private int VIEW_BAD    =4;
	/** 显示断网 **/
	private int VIEW_WIFIFAILUER=5;
	/** 显示加载数据失败 **/
	private int VIEW_LOADFAILURE=6;
	/**显示加载中**/
	private int VIEW_LOADING	=7;
	/**显示请求超时**/
	private int VIEW_OUTTIME    =8;
	/**显示没有数据**/
	private int VIEW_NOCOMMENTS =9;
	/**显示开始加载数据**/
	private int VIEW_COMMENT_LOADING  =10;
	private int VIEW_NO_SHOPSCOMMENTS =11;
	private int VIEW_NO_BADCOMMENTS   =12;
	
	private LinearLayout allFooter;    		//全部评论最后一条listview
	private TextView aFooter;
	private LinearLayout goodFooter;   	 	//好评最后一条listview
	private TextView gFooter;
	private LinearLayout middleFooter;   	//中评最后一条listview
	private TextView mFooter;
	private LinearLayout badFooter;    		//差评最后一条listview
	private TextView bFooter;
	
	public ShopInfoCommentLayout(Context context) {
		super(context);
	}

	public ShopInfoCommentLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ShopInfoCommentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
	
	private View view;
	private void findView() {
		if( view != null )
			return;
		
		//初始化控件
		view = View.inflate(getContext(), R.layout.layout_comment, this);
		all = (TextView) view.findViewById(R.id.comment_all);
		good = (TextView) view.findViewById(R.id.comment_good);
		middle = (TextView) view.findViewById(R.id.comment_middle);
		bad = (TextView) view.findViewById(R.id.comment_bad);

		mHeadErrorHintView = (ErrorHintView) view.findViewById(R.id.head_errorHintView);
		mMiddleErrorHintView = (ErrorHintView) view.findViewById(R.id.middle_errorHintView);
		mBadErrorHintView = (ErrorHintView) view.findViewById(R.id.bad_errorHintView);
		
		allList = (PullToRefreshListView) view.findViewById(R.id.comment_listview_all);
		goodList = (PullToRefreshListView) view.findViewById(R.id.comment_listview_good);
		middleList = (PullToRefreshListView) view.findViewById(R.id.comment_listview_middle);
		badList = (PullToRefreshListView) view.findViewById(R.id.comment_listview_bad);
		
		// 设置监听器
		all.setOnClickListener(this);
		good.setOnClickListener(this);
		middle.setOnClickListener(this);
		bad.setOnClickListener(this);
		
		mShopsModel = ShopsModel_.getInstance_(getContext());
		//初始化listview
		setScrollerListener();
	}
	
	/**
	 * UI初始化
	 */
	public void initView( ShopItem shopItem ) {
		findView();
		item = shopItem;
		
		if( allAdapter!= null ){
			allAdapter.notifyDataSetChanged();
			allList.getRefreshableView().setSelection(0);
		}
		if( goodAdapter!=null ){
			goodAdapter.notifyDataSetChanged();
			goodList.getRefreshableView().setSelection(0);
		}
		if( middleAdapter!=null ){
			middleAdapter.notifyDataSetChanged();
			middleList.getRefreshableView().setSelection(0);
		}
		if( badAdapter!=null ){
			badAdapter.notifyDataSetChanged();
			badList.getRefreshableView().setSelection(0);
		}
		
		all.setBackgroundResource(R.drawable.comments_pressed_bg);
		good.setBackgroundResource(R.drawable.comments_normal_bg);
		middle.setBackgroundResource(R.drawable.comments_normal_bg);
		bad.setBackgroundResource(R.drawable.comments_normal_bg);
		all.setTextColor(Color.WHITE);
		good.setTextColor(Color.BLACK);
		middle.setTextColor(Color.BLACK);
		bad.setTextColor(Color.BLACK);
		
		showViewById(VIEW_COMMENT_LOADING);
	}
	
	/**
	 * 初始化listview的监听器
	 */
	private void setScrollerListener() {
		allAdapter = new ShopCommentAdapter(getContext(), allData);
		allList.setAdapter(allAdapter);
		allList.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.isHeaderShown()){
					allNext = 1;
					loadAllCommentsData(true);
				}
			}
		});
		allList.setOnPreRefreshingAnimListener(new OnPreRefreshingAnimListener() {

			@Override
			public void onPreRefreshingAnim() {
				allList.setFooterLoadingViewHeaderText("加载更多评论");
				
			}
		});
		allList.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if( allNext == 0 ){
					//没有下一页了
					allList.onRefreshComplete();
					if ( allFooter == null ) {
						allFooter = (LinearLayout) View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
						aFooter = (TextView) allFooter.findViewById(R.id.footer);
						allList.getRefreshableView().addFooterView(allFooter);
						// aFooter.setText("到底了呢-共" + allData.size() + "条评论");
						aFooter.setText("到底了呢");
					}
				}else{
					//滑动到底部自动刷新
					loadAllCommentsData(false);
				}
			}
		});
		
		goodAdapter = new ShopCommentAdapter(getContext(), goodData);
		goodList.setAdapter(goodAdapter);
		goodList.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.isHeaderShown()){
					goodNext = 1;
					loadGoodCommentsData(true);
				}
			}
		});
		goodList.setOnPreRefreshingAnimListener(new OnPreRefreshingAnimListener() {

			@Override
			public void onPreRefreshingAnim() {
				goodList.setFooterLoadingViewHeaderText("加载更多评论");
				
			}
		});
		goodList.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if( goodNext == 0 ){
					//没有下一页了
					goodList.onRefreshComplete();
					if ( goodFooter == null ) {
						goodFooter = (LinearLayout) View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
						gFooter = (TextView) goodFooter.findViewById(R.id.footer);
						goodList.getRefreshableView().addFooterView(goodFooter);
						// gFooter.setText("到底了呢-共" + goodData.size() + "条评论");
						gFooter.setText("到底了呢");
					}
				}else{
					//滑动到底部自动刷新
					loadGoodCommentsData(false);
				}
			}
		});
		
		middleAdapter = new ShopCommentAdapter(getContext(), middleData);
		middleList.setAdapter(middleAdapter);
		middleList.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.isHeaderShown()){
					middleNext = 1;
					loadMiddleCommentsData(true);
				}
			}
		});
		middleList.setOnPreRefreshingAnimListener(new OnPreRefreshingAnimListener() {

			@Override
			public void onPreRefreshingAnim() {
				middleList.setFooterLoadingViewHeaderText("加载更多评论");
				
			}
		});
		middleList.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if( middleNext == 0 ){
					//没有下一页了
					middleList.onRefreshComplete();
					if ( middleFooter == null ) {
						middleFooter = (LinearLayout) View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
						mFooter = (TextView) middleFooter.findViewById(R.id.footer);
						middleList.getRefreshableView().addFooterView(middleFooter);
						// mFooter.setText("到底了呢-共" + middleData.size() + "条评论");
						mFooter.setText("到底了呢");
					}
				}else{
					//滑动到底部自动刷新
					loadMiddleCommentsData(false);
				}
			}
		});
		
		badAdapter = new ShopCommentAdapter(getContext(), badData);
		badList.setAdapter(badAdapter);
		badList.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.isHeaderShown()){
					badNext = 1;
					loadBadCommentsData(true);
				}
			}
		});
		badList.setOnPreRefreshingAnimListener(new OnPreRefreshingAnimListener() {

			@Override
			public void onPreRefreshingAnim() {
				badList.setFooterLoadingViewHeaderText("加载更多评论");
				
			}
		});
		badList.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if( badNext == 0 ){
					//没有下一页了
					badList.onRefreshComplete();
					if ( badFooter == null ) {
						badFooter = (LinearLayout) View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
						bFooter = (TextView) badFooter.findViewById(R.id.footer);
						badList.getRefreshableView().addFooterView(badFooter);
						// bFooter.setText("到底了呢-共" + badData.size() + "条评论");
						bFooter.setText("到底了呢");
					}
				}else{
					//滑动到底部自动刷新
					loadBadCommentsData(false);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.comment_all:
			
			if( allData.size()==0){
				showViewById(VIEW_LOADING);
				loadAllCommentsData(false);
			} else {
				allList.getRefreshableView().setSelection(0);
				showViewById(VIEW_ALL);
			}
			
			position = 0;
			all.setBackgroundResource(R.drawable.comments_pressed_bg);
			good.setBackgroundResource(R.drawable.comments_normal_bg);
			middle.setBackgroundResource(R.drawable.comments_normal_bg);
			bad.setBackgroundResource(R.drawable.comments_normal_bg);
			all.setTextColor(Color.WHITE);
			good.setTextColor(Color.BLACK);
			middle.setTextColor(Color.BLACK);
			bad.setTextColor(Color.BLACK);
			
			break;

		case R.id.comment_good:
			
			if( goodData.size()==0 ){
				showViewById(VIEW_LOADING);
				loadGoodCommentsData(true);
			}  else {
				showViewById(VIEW_GOOD);
				goodList.getRefreshableView().setSelection(0);
			}
			
			position = 1;
			all.setBackgroundResource(R.drawable.comments_normal_bg);
			good.setBackgroundResource(R.drawable.comments_pressed_bg);
			middle.setBackgroundResource(R.drawable.comments_normal_bg);
			bad.setBackgroundResource(R.drawable.comments_normal_bg);
			all.setTextColor(Color.BLACK);
			good.setTextColor(Color.WHITE);
			middle.setTextColor(Color.BLACK);
			bad.setTextColor(Color.BLACK);
			
			break;
			
		case R.id.comment_middle:

			if( middleData!=null && middleData.size()==0){
				showViewById(VIEW_LOADING);
				loadMiddleCommentsData(true);
			} else {
				showViewById(VIEW_MIDDLE);
				middleList.getRefreshableView().setSelection(0);
			}

			position = 2;
			all.setBackgroundResource(R.drawable.comments_normal_bg);
			good.setBackgroundResource(R.drawable.comments_normal_bg);
			middle.setBackgroundResource(R.drawable.comments_pressed_bg);
			bad.setBackgroundResource(R.drawable.comments_normal_bg);
			all.setTextColor(Color.BLACK);
			good.setTextColor(Color.BLACK);
			middle.setTextColor(Color.WHITE);
			bad.setTextColor(Color.BLACK);
			
			break;
			
		case R.id.comment_bad:

			if( badData.size()==0 ){
				showViewById(VIEW_LOADING);
				loadBadCommentsData(true);
			} else {
				showViewById(VIEW_BAD);
				badList.getRefreshableView().setSelection(0);
			}
			
			position = 3;
			all.setBackgroundResource(R.drawable.comments_normal_bg);
			good.setBackgroundResource(R.drawable.comments_normal_bg);
			middle.setBackgroundResource(R.drawable.comments_normal_bg);
			bad.setBackgroundResource(R.drawable.comments_pressed_bg);
			all.setTextColor(Color.BLACK);
			good.setTextColor(Color.BLACK);
			middle.setTextColor(Color.BLACK);
			bad.setTextColor(Color.WHITE);
			break;
		default:
			break;
		}
	}
	
	
	/**
	 * 获取全部的评论数据
	 * @param flag  false列表不置顶
	 */
	public void loadAllCommentsData (final boolean flag) {
		if ( !NetWorkCenter.isNetworkConnected(mContext)) {
			showViewById(VIEW_WIFIFAILUER);
			return;
		}
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( allFooter!=null ){
					allList.getRefreshableView().removeFooterView(allFooter);
					allFooter = null;
				}
			}
		});
		if( allNext == 0 ){
			mActivity.runOnUiThread(new  Runnable() {
				public void run() {
					allList.onRefreshComplete();
				}
			});
			return;
		}
		
		ThreadPoolManager_.getInstance_(getContext()).executeTask(new Runnable() {
			@Override
			public void run() {
				Result<Comments> result = null;
				try {
					CommentsReq commentsReq = new CommentsReq();
					if(item!=null){
						commentsReq.setsId(item.getsId());
					}
					commentsReq.setFilter(0);
					commentsReq.setPage(allNext);
					result = mShopsModel.v2_1queryComments(commentsReq);
					
					if( result!=null && result.getCode() == 1 ){
						final Comments bean = result.getData();
						if ( bean==null ){
							mActivity.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									showViewById(VIEW_NO_SHOPSCOMMENTS);
								}
							});
						} else {
							if( bean.getAllNum()==0 || bean.getList()==null || bean.getList().length==0 ) {
								mActivity.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										showViewById(VIEW_NO_SHOPSCOMMENTS);
									}
								});								
							} else {
								refreshAllUI(bean, flag);
							}
						}
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						return;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						return;
					case NETWORK:
						showViewById(VIEW_OUTTIME);
						return;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						return;
					} 
				} finally {
					mActivity.runOnUiThread(new  Runnable() {
						public void run() {
							allList.onRefreshComplete();
						}
					});
				}
			}
		});
	}
	
	private void refreshAllUI(final Comments bean, final boolean flag){
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( flag ) {
					allData.clear();
				}
				List<CommentsCon> list = Arrays.asList(bean.getList());
				int size = list.size();
				for (int i = 0; i < size; i++) {
					CommentsCon con = list.get(i);
					if( !TextUtils.isEmpty(con.getComments()) ) {
						allData.add(con);
					}
				}
				
				if ( allData!=null && allData.size()>0 ) {
					showViewById(VIEW_ALL);
					allNext = bean.getPage();
					allAdapter.notifyDataSetChanged();
					if ( flag ) {
						allList.getRefreshableView().setSelection(0);
					}
				} else {
					showViewById(VIEW_NO_SHOPSCOMMENTS);
				}
			}
			
		});
	}
	
	/**
	 * 获取好评的数据
	 * @param filter  过滤器（0全部，1好评，2中评，3差评）默认0全部
	 */
	public void loadGoodCommentsData (final boolean flag) {
		if ( !NetWorkCenter.isNetworkConnected(mContext)) {
			showViewById(VIEW_WIFIFAILUER);
			return;
		}
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( goodFooter!=null ){
					goodList.getRefreshableView().removeFooterView(goodFooter);
					goodFooter = null;
				}
			}
		});
		if( goodNext == 0 ){
			mActivity.runOnUiThread(new  Runnable() {
				public void run() {
					goodList.onRefreshComplete();
				}
			});
			return;
		}
		
		ThreadPoolManager_.getInstance_(getContext()).executeTask(new Runnable() {
			@Override
			public void run() {
				Result<Comments> result = null;
				try {
					CommentsReq commentsReq = new CommentsReq();
					commentsReq.setsId(item.getsId());
					commentsReq.setFilter(1);
					commentsReq.setPage(goodNext);
					result = mShopsModel.v2_1queryComments(commentsReq);
					if( result!=null && result.getCode() == 1 ){
						final Comments bean = result.getData();
						if ( bean==null ){
							mActivity.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									showViewById(VIEW_NOCOMMENTS);
								}
							});
						} else {
							if ( bean.getList()==null || bean.getList().length==0 ) {
								mActivity.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										showViewById(VIEW_NOCOMMENTS);
									}
								});
							} else {
								refreshGoodUI(bean, flag);
							}
						}
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						return;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						return;
					case NETWORK:
						showViewById(VIEW_OUTTIME);
						return;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						return;
					}
				} finally {
					mActivity.runOnUiThread(new  Runnable() {
						public void run() {
							goodList.onRefreshComplete();
						}
					});
				}
			}
		});
	}
	
	private void refreshGoodUI(final Comments bean, final boolean flag){
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( flag ) {
					goodData.clear();
				}
				List<CommentsCon> list = Arrays.asList(bean.getList());
				int size = list.size();
				for (int i = 0; i < size; i++) {
					CommentsCon con = list.get(i);
					if( !TextUtils.isEmpty(con.getComments()) ) {
						goodData.add(con);
					}
				}

				if ( goodData!=null && goodData.size()>0 ) {
					showViewById(VIEW_GOOD);
					goodNext = bean.getPage();
					goodAdapter.notifyDataSetChanged();
				} else {
					showViewById(VIEW_NOCOMMENTS);
				}
			}
			
		});
	}
	
	/**
	 * 获取中评的数据
	 * @param filter  过滤器（0全部，1好评，2中评，3差评）默认0全部
	 */
	public void loadMiddleCommentsData (final boolean flag) {
		if ( !NetWorkCenter.isNetworkConnected(mContext)) {
			showViewById(VIEW_WIFIFAILUER);
			return;
		}
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( middleFooter!=null ){
					middleList.getRefreshableView().removeFooterView(middleFooter);
					middleFooter = null;
				}
			}
		});
		if( middleNext == 0 ){
			mActivity.runOnUiThread(new  Runnable() {
				public void run() {
					middleList.onRefreshComplete();
				}
			});
			return;
		}
		
		ThreadPoolManager_.getInstance_(getContext()).executeTask(new Runnable() {
			@Override
			public void run() {
				Result<Comments> result = null;
				try {
					CommentsReq commentsReq = new CommentsReq();
					commentsReq.setsId(item.getsId());
					commentsReq.setFilter(2);
					commentsReq.setPage(middleNext);
					result = mShopsModel.v2_1queryComments(commentsReq);
					if( result != null && result.getCode() == 1 ){
						final Comments bean = result.getData();
						if ( bean==null ){
							mActivity.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									showViewById(VIEW_NOCOMMENTS);
								}
							});
						} else {
							if ( bean.getList()==null || bean.getList().length==0 ) {
								mActivity.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										showViewById(VIEW_NOCOMMENTS);
									}
								});
							} else {
								refreshMiddleUI(bean, flag);
							}
						}
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						return;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						return;
					case NETWORK:
						showViewById(VIEW_OUTTIME);
						return;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						return;
					}
				} finally {
					mActivity.runOnUiThread(new  Runnable() {
						public void run() {
							middleList.onRefreshComplete();
						}
					});
				}
			}
		});
	}
	
	private void refreshMiddleUI(final Comments bean, final boolean flag) {

		if (flag) {
			middleData.clear();
		}
		List<CommentsCon> list = Arrays.asList(bean.getList());
		int size = list.size();
		for (int i = 0; i < size; i++) {
			CommentsCon con = list.get(i);
			if (!TextUtils.isEmpty(con.getComments())) {
				middleData.add(con);
			}
		}

		if (middleData != null && middleData.size() > 0) {
			mActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					showViewById(VIEW_MIDDLE);
					middleNext = bean.getPage();
					middleAdapter.notifyDataSetChanged();
				}
			});
		} else {
			mActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					showViewById(VIEW_NOCOMMENTS);
				}
			});
		}

	}
	
	/**
	 * 获取差评的数据
	 * @param filter  过滤器（0全部，1好评，2中评，3差评）默认0全部
	 */
	public void loadBadCommentsData (final boolean flag) {
		if ( !NetWorkCenter.isNetworkConnected(mContext)) {
			showViewById(VIEW_WIFIFAILUER);
			return;
		}
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( badFooter!=null ){
					badList.getRefreshableView().removeFooterView(badFooter);
					badFooter = null;
				}
			}
		});
		if( badNext == 0 ){
			mActivity.runOnUiThread(new  Runnable() {
				public void run() {
					badList.onRefreshComplete();
				}
			});						
			return;
		}
		
		ThreadPoolManager_.getInstance_(getContext()).executeTask(new Runnable() {
			@Override
			public void run() {
				Result<Comments> result = null;
				try {
					CommentsReq commentsReq = new CommentsReq();
					commentsReq.setsId(item.getsId());
					commentsReq.setFilter(3);
					commentsReq.setPage(badNext);
					result = mShopsModel.v2_1queryComments(commentsReq);
					if( result!=null && result.getCode() == 1 ){
						final Comments bean = result.getData();
						if ( bean==null ){
							showViewById(VIEW_NO_BADCOMMENTS);
						} else {
							if ( bean.getList()==null || bean.getList().length==0 ) {
								showViewById(VIEW_NO_BADCOMMENTS);
							} else {
								refreshBadUI(bean, flag);
							}
						}
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						return;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						return;
					case NETWORK:
						showViewById(VIEW_OUTTIME);
						return;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						return;
					}
				} finally {
					mActivity.runOnUiThread(new  Runnable() {
						public void run() {
							badList.onRefreshComplete();
						}
					});
				}
				
			}
		});
	}
	
	private void refreshBadUI(final Comments bean, final boolean flag) {
		if (flag) {
			badData.clear();
		}
		List<CommentsCon> list = Arrays.asList(bean.getList());
		int size = list.size();
		for (int i = 0; i < size; i++) {
			CommentsCon con = list.get(i);
			if (!TextUtils.isEmpty(con.getComments())) {
				badData.add(con);
			}
		}

		if (badData != null && badData.size() > 0) {
			mActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					showViewById(VIEW_BAD);
					badNext = bean.getPage();
					badAdapter.notifyDataSetChanged();
				}
			});
		} else {

			showViewById(VIEW_NO_BADCOMMENTS);

		}

	}

	public void clearCommentsList(){
		if( view == null )
			return;
		allData.clear();
		goodData.clear();
		middleData.clear();
		badData.clear();
		
		allNext = 1;
		goodNext = 1;
		middleNext = 1;
		badNext = 1;
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( allFooter!=null ){
					allList.getRefreshableView().removeFooterView(allFooter);
					allFooter = null;
				}
			}
		});
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( goodFooter!=null ){
					goodList.getRefreshableView().removeFooterView(goodFooter);
					goodFooter = null;
				}
			}
		});
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( middleFooter!=null ){
					middleList.getRefreshableView().removeFooterView(middleFooter);
					middleFooter = null;
				}
			}
		});
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if ( badFooter!=null ){
					badList.getRefreshableView().removeFooterView(badFooter);
					badFooter = null;
				}
			}
		});
		position = 0;
		item = null;
	}
	
	/**
	 * 显示全部评论：1
	 * 显示好评：2
	 * 显示中评：3
	 * 显示差评：4
	 * 显示断网：5
	 * 显示加载数据失败：6
	 * 显示加载中：7
	 * 显示请求超时：8
	 * 显示没有数据：9
	 * 显示开始的加载 :10
	 * 显示这家店没有评论:11
	 */
	private void showViewById(final int i){
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mHeadErrorHintView.setVisibility(View.GONE);
				mMiddleErrorHintView.setVisibility(View.GONE);
				mBadErrorHintView.setVisibility(View.GONE);
				allList.setVisibility(View.GONE);
				goodList.setVisibility(View.GONE);
				middleList.setVisibility(View.GONE);
				badList.setVisibility(View.GONE);
				switch(i){
				case 1:
					mHeadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					allList.setVisibility(View.VISIBLE);
					break;
				case 2:
					mHeadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					goodList.setVisibility(View.VISIBLE);
					break;
				case 3:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					middleList.setVisibility(View.VISIBLE);
					break;
				case 4:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					badList.setVisibility(View.VISIBLE);
					break;
				case 5:
					mBadErrorHintView.hideLoading();
					mHeadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mMiddleErrorHintView.netError(new OperateListener() {
						@Override
						public void operate() {
							retry();
						}
					});
					break;
				case 6:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mMiddleErrorHintView.loadFailure(new OperateListener() {
						@Override
						public void operate() {
							retry();
						}
					});
					break;
				case 7:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.loadingData();
					break;
				case 8:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mMiddleErrorHintView.timeOut(new OperateListener() {
						@Override
						public void operate() {
							retry();
						}
					});
					break;
				case 9:
					mHeadErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mMiddleErrorHintView.noComments("暂无此类评论");
					break;
				case 10:
					mHeadErrorHintView.loadingData();
					mBadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					break;
				case 11:
					mHeadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mHeadErrorHintView.noComments("还没人评论这个店呢");
					break;
				case 12:
					mHeadErrorHintView.hideLoading();
					mMiddleErrorHintView.hideLoading();
					mBadErrorHintView.hideLoading();
					mBadErrorHintView.noComments("这家店真牛,一条差评都没有");
					break;
				}   
			}
		});

	}
	
	/**
	 * 重连操作
	 */
	private void retry(){
		showViewById(VIEW_LOADING);
		if ( position == 0 ){
			allData.clear();
			loadAllCommentsData(false);
		} else if( position == 1 ) {
			goodData.clear();
			loadGoodCommentsData(false);
		} else if ( position == 2 ){
			middleData.clear();
			loadMiddleCommentsData(false);
		} else if( position == 3 ){
			badData.clear();
			loadBadCommentsData(false);
		} else {
			allData.clear();
			loadAllCommentsData(true);
		}
	}
	
		
}
